﻿<cfsilent>
	<cfscript>
		
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		
		examId = event.getArg("EXID");

		/* 加载考试任务 */
	
		sql = "SELECT a.wdidx,
					   a.cellidx,
					   a.cid,
					   a.term_id,
					   a.rstidx,
					   a.wid,
					   a.examer_sum,
					   a.campus_id,
					   b.course_name 
				FROM t_resittask a 
             		INNER JOIN t_course b ON b.cid = a.cid 
				WHERE a.task_exrstid = :examId ";
				
		queryObj = new Query( datasource=application.dnsSlave ) ;
   		queryObj.addParam( name="examId", value=examId, cfsqltype="cf_sql_char" );
		
		rs_examTask= queryObj.execute( sql=sql ).getResult();

		/* 设置考试可用的时间 */
		timeTable = arrayNew(2);
		
		for(weekDay=1; weekDay lte 7; weekDay++){
			for(lecture=1; lecture lte 5; lecture++){
				timeTable[weekDay][lecture] = 0;
			}
		}

		/* 根据考试类型 选择加载时间设置数据 */

		/* 读取半期考试可用时间 */
		/* 第6位 是否用于补考 */
		
		sql = "SELECT a.wdidx, a.lecidx, a.props 
						FROM t_timetable a  
						WHERE 
							  SUBSTR(a.props, 6, 1)='1' 
							  AND 
							  a.lecidx IN ('1','2','3','4','5')";
				
		queryObj = new Query( datasource=application.dnsSlave) ;
		
		rs_timeSet= queryObj.execute( sql=sql ).getResult();
		
		for (   a=1; a LTE rs_timeSet.recordCount; a++   ) {
			timeTable[rs_timeSet["wdidx"][a]][rs_timeSet["lecidx"][a]] = 1;
		}
		
		/* 如果当前已排考 则标注当前考试安排 */
		if ( rs_examTask.wdidx neq "0" ) {
			timeTable[rs_examTask.wdidx][rs_examTask.cellidx] = -1;
		}

		/* 检查现有考试冲突 */
		
		termId = rs_examTask.term_id;
		examWeek = rs_examTask.wid;
		
		courseId=rs_examTask.cid;
		examIdx=rs_examTask.rstidx;
		
		sql = "SELECT a.task_id 
				FROM t_resitplan a  
				WHERE a.cid = :courseId  
					  AND 
					  a.term_id = :termId 
					  AND 
					  a.rstidx = :examIdx ";
					  
		queryObj = new Query( datasource=application.dnsSlave ) ;
   		queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="examIdx", value=examIdx, cfsqltype="cf_sql_char" );
		
		rs_taskIds= queryObj.execute( sql=sql ).getResult();
		
		/* 学生不能同时安排两场考试 */
								
		sql = "SELECT 
				a.wdidx, a.cellidx 
				FROM t_resittask a
				WHERE 
					( a.task_exrstid > :examId 
					  OR 
					  a.task_exrstid < :examId 
					 ) 
					AND 
					a.wdidx > '0' 
					AND 
					a.wid = :examWeek 
					AND 
					a.term_id = :termId 
					AND 
					EXISTS (
						SELECT 1
						FROM t_resitplan b
						WHERE 
							a.cid = b.cid 
							AND 
							a.term_id = b.term_id 
							AND 
							a.rstidx = b.rstidx 
							AND 
							EXISTS (
								  SELECT 1
								  FROM t_temp_mark_resit e 
								  WHERE 
									e.task_id= b.task_id 
									AND 
									EXISTS (
									  SELECT 1
										FROM t_temp_mark_resit d
										WHERE 
											d.stu_id = e.stu_id
											AND 
											d.task_id IN ( :taskIds )
									
									)
							)
						)";
						
		queryObj = new Query( datasource=application.dnsSlave ) ;
   		queryObj.addParam( name="taskIds", value=arrayToList(rs_taskIds["task_id"],"|"), cfsqltype="cf_sql_varchar",list = true ,separator="|");
		queryObj.addParam( name="examWeek", value=examWeek, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="examId", value=examId, cfsqltype="cf_sql_varchar" );
		
		rs_conflictExam= queryObj.execute( sql=sql ).getResult();
		
		for (   a=1; a LTE rs_conflictExam.recordCount; a++   ) {
			timeTable[rs_conflictExam["wdidx"][a]][rs_conflictExam["cellidx"][a]] = -3;
		}
		

		/* 检查教室是否足够 */
		studentCount = rs_examTask.examer_sum;
		campusId = rs_examTask.campus_id;
		termId = rs_examTask.term_id;
		examWeek = rs_examTask.wid;
		
		/* 进行开学前学期修正 */
		if ( examWeek eq -1 ) { examWeek = 25; }

		for(weekDay=1; weekDay lte 7; weekDay++){
			for(lecture=1; lecture lte 5; lecture++){
				
				if ( timeTable[weekDay][lecture] eq 1 ) {
					
					cells = 2;
					/* 如果为 第二讲 或 第五讲 则使用最长学时数 */
					if (lecture eq 2 or lecture eq 5) { cells = 3; }
					
					cellStart = mathAdvice.convertLectureIdxToCellStart(lecture);
					cellEnd = cellStart + cells -1;
				
					sql = "SELECT 
							case when sum(a.sits_exam) is null then 0 else sum(a.sits_exam) end sits_exam
							FROM t_classroom a 
							WHERE 
								SUBSTR(a.cr_usage, 3, 1) = '1' 
								AND 
								(a.campus_id= :campusId )
								AND 
								NOT EXISTS (
									SELECT 1
									FROM t_clsroom_timetable b
									WHERE 
										a.cr_id = b.cr_id
										AND 
										b.term_id = :termId 
										AND 
										b.wdidx = :weekDay 
										AND
										NOT ( b.cellidx + b.cellrelay -1 < :cellStart 
												OR 
												b.cellidx > :cellEnd 
											  ) 
										AND 
										SUBSTR(b.widx, #int(examWeek)#, 1) = '1' 
								)";
								
					queryObj = new Query( datasource=application.dnsSlave ) ;
					queryObj.addParam( name="weekDay", value=weekDay, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="cellStart", value=cellStart, cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="cellEnd", value=cellEnd, cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="campusId", value=campusId, cfsqltype="cf_sql_char" );
					
					rs_clsroomCheck= queryObj.execute( sql=sql ).getResult();

					/* 如果可用考场座位数 没有超过应考人数10 */
					
					if ( rs_clsroomCheck.recordCount ) {
						
						if ( rs_clsroomCheck["sits_exam"][1] lt studentCount + 10 ) {
							timeTable[weekDay][lecture] = -4;
						}
					}
					else {
						timeTable[weekDay][lecture] = -4;
					}
					
				}
			
			}
		}

		
		backArgs = structNew();

		/* 对于 javascript 地址 &amp; 需要重新转义为 & 避免 google chrome 浏览器无法正确识别地址 */
		backURL = replace( buildURL( "examResitArrange", backArgs), "&amp;", "&", "all");

	</cfscript>
</cfsilent>

<table class="UICourseTable">
	<thead>
		<tr>
			<td colspan="2" width="2%"></td>
			<td width="14%">星期一</td>
			<td width="14%">星期二</td>
			<td width="14%">星期三</td>
			<td width="14%">星期四</td>
			<td width="14%">星期五</td>
			<td width="14%">星期六</td>
			<td width="14%">星期天</td>
		</tr>
	</thead>
	<tbody>
		<cfloop from="1" to="5" index="lec">
			<tr>
				<cfswitch expression="#lec#">
					<cfcase value="1">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="3">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="5">
						<td rowspan="1"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
				</cfswitch>
				<td><cfoutput>#mathAdvice.convertExamIdxToChinese(lec)#</cfoutput></td>
				<cfloop from="1" to="7" index="wkday">
					<td <cfif timeTable[wkday][lec] eq 1>class="available"</cfif>>
					
						<!--- 如果学生上课时间冲突 --->
						<cfif timeTable[wkday][lec] eq -2>
							<cfset percent = int(1000 * conflictSet["STU_#wkday#@#lec#"] / studentCount)/10 />
							<cfif percent lt 5>
								<a class="botton force" href="javascript:addTimeCell(<cfoutput>#wkday#</cfoutput>,<cfoutput>#lec#</cfoutput>);">可排</a>
								<br/>
							</cfif>
							<span>有课</span>
							<span class="numeric">(<cfoutput>#conflictSet["STU_#wkday#@#lec#"]#</cfoutput>)</span>
						</cfif>
						
						<!--- 如果学生有考试 --->
						<cfif timeTable[wkday][lec] eq -3>
							<span>考试</span>
						</cfif>
						
						<!--- 如果教室不足 --->
						<cfif timeTable[wkday][lec] eq -4>
							<span>考场不足</span>
						</cfif>
						
						<!--- 如果已安排 --->
						<cfif timeTable[wkday][lec] eq -1>
							<a href="javascript:removeTimeCell(<cfoutput>#wkday#</cfoutput>,<cfoutput>#lec#</cfoutput>);"><cfoutput>#rs_examTask.course_name#</cfoutput></a>
						</cfif>
						
						<!--- 如果时间可用 --->
						<cfif timeTable[wkday][lec] eq 1>
							<a class="botton yes" href="javascript:addTimeCell(<cfoutput>#wkday#</cfoutput>,<cfoutput>#lec#</cfoutput>);">可排</a>
						</cfif>
						
						<!--- 不允许排考 --->
						<cfif timeTable[wkday][lec] eq 0>
							<span>禁止排考</span>
						</cfif>
						
					</td>
				</cfloop>
			</tr>
		</cfloop>
	</tbody>
</table>
<div id="out"></div>
<script language="javascript" type="text/javascript">
	//<![CDATA[
	<!--
	
	function removeTimeCell(weekDay, lecture){
		
		$.post(
				"<cfoutput>#buildURL('apiExamResitTaskClear')#</cfoutput>", 
				{
					'exid' : '<cfoutput>#examId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {
					
					//$("#out").html(data);

					result = $.parseJSON(data);
										
					if (result.success) {
						/* 刷新排课数据 */
						window.location.reload();
					}
					else {
						alert(result.reason);
					}
					
				});
		
		
	}
	
	function addTimeCell(weekDay, lecture){
		
		$.post(
				"<cfoutput>#buildURL('apiExamResitTaskSetTime')#</cfoutput>", 
				{
					'wkday' : weekDay,
					'lec' : lecture,
					'exid' : '<cfoutput>#examId#</cfoutput>',
					'seed': (new Date()).getTime()
				}, 
				function(data) {

					//$("#out").html(data);

					result = $.parseJSON(data);
					
					if (result.success) {

						/* 返回排课任务列表 */
						window.location = '<cfoutput>#backURL#</cfoutput>';
					
					}
					else {
						alert(result.reason);
					}

				});
		
	}	
	
	// -->
	//]]>
</script>
	